/**
  ******************************************************************************
  * @file     DCMotor_PWM6612_Driver.c
  * @author		HpDadi 
  * @version  V1.0
  * @date     18-June-2018
  * @brief    This is a Driver based on the chip 6612, a double H brige PWM DCmotor Driver.      
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "DCMotor_PWM6612_Driver.h"

/** @addtogroup DCMotor_PWM6612_Driver
  * @{
  */

/** @defgroup Driver_Private_Variables
  * @{
	*/
uint16_t DCMotor_PWM6612_PWM_Period;

/**
  * @}
  */
	

/** @defgroup Driver_Private_Functions
  * @{
  */

/**
  * @brief 
  * @param  
  * @retval 
  */
void DCMotor_PWM6612_Init(uint8_t DCMotor_PWM6612_Port, uint16_t PWM_Period, uint16_t DCMotor_PWM6612_Port_InitVoltage)
{
	DCMotor_PWM6612_PWM_Period = PWM_Period;
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = DCMotor_PWM6612_STBY_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	RCC_APB2PeriphClockCmd(DCMotor_PWM6612_STBY_Clock,ENABLE);
	GPIO_Init(DCMotor_PWM6612_STBY_Port, &GPIO_InitStructure);
	
	#ifdef DCMotor_PWM6612_UseA
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortA)
	{
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_InitStructure.GPIO_Pin = DCMotor_PWM6612_AIN1_Pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
		RCC_APB2PeriphClockCmd(DCMotor_PWM6612_AIN1_Clock,ENABLE);
		GPIO_Init(DCMotor_PWM6612_AIN1_Port, &GPIO_InitStructure);
	
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_InitStructure.GPIO_Pin = DCMotor_PWM6612_AIN2_Pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
		RCC_APB2PeriphClockCmd(DCMotor_PWM6612_AIN2_Clock,ENABLE);
		GPIO_Init(DCMotor_PWM6612_AIN2_Port, &GPIO_InitStructure);
		
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Pin = DCMotor_PWM6612_PWMA_CH_Pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		RCC_APB2PeriphClockCmd(DCMotor_PWM6612_PWMA_CH_Clock,ENABLE);
		GPIO_Init(DCMotor_PWM6612_PWMA_CH_Port, &GPIO_InitStructure);
				
		TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrcture;
		TIM_TimeBaseInitStrcture.TIM_ClockDivision = TIM_CKD_DIV1;
		TIM_TimeBaseInitStrcture.TIM_CounterMode = TIM_CounterMode_Up;
		TIM_TimeBaseInitStrcture.TIM_Period = 1000000/PWM_Period-1;
		TIM_TimeBaseInitStrcture.TIM_Prescaler = 72-1;
		if(DCMotor_PWM6612_PWMA_TIM == TIM1){
			RCC_APB2PeriphClockCmd(DCMotor_PWM6612_PWMA_Clock,ENABLE);
		}else{
			RCC_APB1PeriphClockCmd(DCMotor_PWM6612_PWMA_Clock,ENABLE);
		}
		TIM_TimeBaseInit(DCMotor_PWM6612_PWMA_TIM,&TIM_TimeBaseInitStrcture);
		
		TIM_OCInitTypeDef TIM_OCInitStructure;
		TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		TIM_OCInitStructure.TIM_Pulse = DCMotor_PWM6612_Port_InitVoltage*10000/PWM_Period;
		#if (DCMotor_PWM6612_PWMA_CH == 0x01)
		TIM_OC1Init(DCMotor_PWM6612_PWMA_TIM,&TIM_OCInitStructure);
		TIM_OC1PreloadConfig(DCMotor_PWM6612_PWMA_TIM,TIM_OCPreload_Disable);
		#endif
		#if (DCMotor_PWM6612_PWMA_CH == 0x02)
		TIM_OC2Init(DCMotor_PWM6612_PWMA_TIM,&TIM_OCInitStructure);
		TIM_OC2PreloadConfig(DCMotor_PWM6612_PWMA_TIM,TIM_OCPreload_Disable);
		#endif
		#if (DCMotor_PWM6612_PWMA_CH == 0x03)
		TIM_OC3Init(DCMotor_PWM6612_PWMA_TIM,&TIM_OCInitStructure);
		TIM_OC3PreloadConfig(DCMotor_PWM6612_PWMA_TIM,TIM_OCPreload_Disable);
		#endif
		#if (DCMotor_PWM6612_PWMA_CH == 0x04)
		TIM_OC4Init(DCMotor_PWM6612_PWMA_TIM,&TIM_OCInitStructure);
		TIM_OC4PreloadConfig(DCMotor_PWM6612_PWMA_TIM,TIM_OCPreload_Disable);
		#endif
		
		TIM_Cmd(DCMotor_PWM6612_PWMA_TIM,ENABLE);
		if(DCMotor_PWM6612_PWMA_TIM == TIM1)
		{
			TIM_CtrlPWMOutputs(DCMotor_PWM6612_PWMA_TIM,ENABLE);
		}
	}
	
	#endif
	
	
	#ifdef DCMotor_PWM6612_UseB
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortB)
	{
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_InitStructure.GPIO_Pin = DCMotor_PWM6612_BIN1_Pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
		RCC_APB2PeriphClockCmd(DCMotor_PWM6612_BIN1_Clock,ENABLE);
		GPIO_Init(DCMotor_PWM6612_BIN1_Port, &GPIO_InitStructure);
	
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
		GPIO_InitStructure.GPIO_Pin = DCMotor_PWM6612_BIN2_Pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
		RCC_APB2PeriphClockCmd(DCMotor_PWM6612_BIN2_Clock,ENABLE);
		GPIO_Init(DCMotor_PWM6612_BIN2_Port, &GPIO_InitStructure);
		
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Pin = DCMotor_PWM6612_PWMB_CH_Pin;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		RCC_APB2PeriphClockCmd(DCMotor_PWM6612_PWMB_CH_Clock,ENABLE);
		GPIO_Init(DCMotor_PWM6612_PWMB_CH_Port, &GPIO_InitStructure);
				
		TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrcture;
		TIM_TimeBaseInitStrcture.TIM_ClockDivision = TIM_CKD_DIV1;
		TIM_TimeBaseInitStrcture.TIM_CounterMode = TIM_CounterMode_Up;
		TIM_TimeBaseInitStrcture.TIM_Period = 1000000/PWM_Period-1;
		TIM_TimeBaseInitStrcture.TIM_Prescaler = 72-1;
		if(DCMotor_PWM6612_PWMA_TIM == TIM1){
			RCC_APB2PeriphClockCmd(DCMotor_PWM6612_PWMB_Clock,ENABLE);
		}else{
			RCC_APB1PeriphClockCmd(DCMotor_PWM6612_PWMB_Clock,ENABLE);
		}
		TIM_TimeBaseInit(DCMotor_PWM6612_PWMB_TIM,&TIM_TimeBaseInitStrcture);
		
		TIM_OCInitTypeDef TIM_OCInitStructure;
		TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
		TIM_OCInitStructure.TIM_Pulse = DCMotor_PWM6612_Port_InitVoltage*10000/PWM_Period;
		#if (DCMotor_PWM6612_PWMB_CH == 0x01)
		TIM_OC1Init(DCMotor_PWM6612_PWMB_TIM,&TIM_OCInitStructure);
		TIM_OC1PreloadConfig(DCMotor_PWM6612_PWMB_TIM,TIM_OCPreload_Disable);
		#endif
		#if (DCMotor_PWM6612_PWMB_CH == 0x02)
		TIM_OC2Init(DCMotor_PWM6612_PWMB_TIM,&TIM_OCInitStructure);
		TIM_OC2PreloadConfig(DCMotor_PWM6612_PWMB_TIM,TIM_OCPreload_Disable);
		#endif
		#if (DCMotor_PWM6612_PWMB_CH == 0x03)
		TIM_OC3Init(DCMotor_PWM6612_PWMB_TIM,&TIM_OCInitStructure);
		TIM_OC3PreloadConfig(DCMotor_PWM6612_PWMB_TIM,TIM_OCPreload_Disable);
		#endif
		#if (DCMotor_PWM6612_PWMB_CH == 0x04)
		TIM_OC4Init(DCMotor_PWM6612_PWMB_TIM,&TIM_OCInitStructure);
		TIM_OC4PreloadConfig(DCMotor_PWM6612_PWMB_TIM,TIM_OCPreload_Disable);
		#endif
		
		TIM_Cmd(DCMotor_PWM6612_PWMB_TIM,ENABLE);
		if(DCMotor_PWM6612_PWMB_TIM == TIM1)
		{
			TIM_CtrlPWMOutputs(DCMotor_PWM6612_PWMB_TIM,ENABLE);
		}
	}
	#endif
	
	
}

/**
  * @brief 
  * @param  
  * @retval 
  */
void DCMotor_PWM6612_Enable(void)
{
	GPIO_SetBits(DCMotor_PWM6612_STBY_Port,DCMotor_PWM6612_STBY_Pin);
}

/**
  * @brief 
  * @param  
  * @retval 
  */
void DCMotor_PWM6612_Forward(uint8_t DCMotor_PWM6612_Port)
{
	#ifdef DCMotor_PWM6612_UseA
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortA)
	{
		GPIO_ResetBits(DCMotor_PWM6612_AIN1_Port,DCMotor_PWM6612_AIN1_Pin);
		GPIO_SetBits(DCMotor_PWM6612_AIN2_Port,DCMotor_PWM6612_AIN2_Pin);
	}
	#endif
	
	#ifdef DCMotor_PWM6612_UseB
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortB)
	{
		GPIO_ResetBits(DCMotor_PWM6612_BIN1_Port,DCMotor_PWM6612_BIN1_Pin);
		GPIO_SetBits(DCMotor_PWM6612_BIN2_Port,DCMotor_PWM6612_BIN2_Pin);
	}
	#endif
}

/**
  * @brief 
  * @param  
  * @retval 
  */
void DCMotor_PWM6612_Reverse(uint8_t DCMotor_PWM6612_Port)
{
	#ifdef DCMotor_PWM6612_UseA
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortA)
	{
		GPIO_ResetBits(DCMotor_PWM6612_AIN2_Port,DCMotor_PWM6612_AIN2_Pin);
		GPIO_SetBits(DCMotor_PWM6612_AIN1_Port,DCMotor_PWM6612_AIN1_Pin);
	}
	#endif
	
	#ifdef DCMotor_PWM6612_UseB
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortB)
	{
		GPIO_ResetBits(DCMotor_PWM6612_BIN2_Port,DCMotor_PWM6612_BIN2_Pin);
		GPIO_SetBits(DCMotor_PWM6612_BIN1_Port,DCMotor_PWM6612_BIN1_Pin);
	}
	#endif
}

/**
  * @brief 
  * @param  
  * @retval 
  */
void DCMotor_PWM6612_Pause(uint8_t DCMotor_PWM6612_Port)
{
	#ifdef DCMotor_PWM6612_UseA
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortA)
	{
		GPIO_ResetBits(DCMotor_PWM6612_AIN2_Port,DCMotor_PWM6612_AIN2_Pin);
		GPIO_ResetBits(DCMotor_PWM6612_AIN1_Port,DCMotor_PWM6612_AIN1_Pin);
	}
	#endif
	
	#ifdef DCMotor_PWM6612_UseB
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortB)
	{
		GPIO_ResetBits(DCMotor_PWM6612_BIN2_Port,DCMotor_PWM6612_BIN2_Pin);
		GPIO_ResetBits(DCMotor_PWM6612_BIN1_Port,DCMotor_PWM6612_BIN1_Pin);
	}
	#endif
}

/**
  * @brief 
  * @param  
  * @retval 
  */
void DCMotor_PWM6612_SetVoltage(uint8_t DCMotor_PWM6612_Port, uint16_t Voltage)
{
	#ifdef DCMotor_PWM6612_UseA
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortA)
	{
		#if (DCMotor_PWM6612_PWMA_CH == 0x01)
		TIM_SetCompare1(DCMotor_PWM6612_PWMA_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
		#if (DCMotor_PWM6612_PWMA_CH == 0x02)
		TIM_SetCompare2(DCMotor_PWM6612_PWMA_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
		#if (DCMotor_PWM6612_PWMA_CH == 0x03)
		TIM_SetCompare3(DCMotor_PWM6612_PWMA_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
		#if (DCMotor_PWM6612_PWMA_CH == 0x04)
		TIM_SetCompare4(DCMotor_PWM6612_PWMA_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
	}
	#endif
	
	#ifdef DCMotor_PWM6612_UseB
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortB)
	{
		#if (DCMotor_PWM6612_PWMB_CH == 0x01)
		TIM_SetCompare1(DCMotor_PWM6612_PWMB_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
		#if (DCMotor_PWM6612_PWMB_CH == 0x02)
		TIM_SetCompare2(DCMotor_PWM6612_PWMB_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
		#if (DCMotor_PWM6612_PWMB_CH == 0x03)
		TIM_SetCompare3(DCMotor_PWM6612_PWMB_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
		#if (DCMotor_PWM6612_PWMB_CH == 0x04)
		TIM_SetCompare4(DCMotor_PWM6612_PWMB_TIM, Voltage*10000/DCMotor_PWM6612_PWM_Period);
		#endif
	}
	#endif
}

/**
  * @brief 
  * @param  
  * @retval 
  */
void DCMotor_PWM6612_SetPWMPeriod(uint8_t DCMotor_PWM6612_Port,uint16_t PWM_Peroid)
{
	DCMotor_PWM6612_PWM_Period = PWM_Peroid;
	#ifdef DCMotor_PWM6612_UseA
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortA)
	{
		TIM_SetAutoreload(DCMotor_PWM6612_PWMA_TIM,PWM_Peroid);
	}
	#endif
	
	#ifdef DCMotor_PWM6612_UseB
	if(DCMotor_PWM6612_Port == DCMotor_PWM6612_PortB)
	{
		TIM_SetAutoreload(DCMotor_PWM6612_PWMB_TIM,PWM_Peroid);
	}
	#endif
}

/**
  * @}
  */

/**
  * @}
  */
